package com.example.mkio.androidpractice.Views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by mk.io on 17-6-12.
 */

public class CustomViewFive extends ViewGroup {
    private static final String TAG = "CustomFive";

    public CustomViewFive(Context context) {
        super(context);
    }

    public CustomViewFive(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomViewFive(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heigthMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heigth = MeasureSpec.getSize(heightMeasureSpec);

        // 计算出所有的childView的宽和高
        /**
         * 通过ViewGroup的measureChildren方法为其所有的孩子设置宽和高，此行执行完成后，childView的宽和高都已经正确的计算过了
         */
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        /**
         * 记录如果是wrap_content是设置的宽和高
         */
        int relWidth = 0;
        int relHeight = 0;

        int mCount = getChildCount();


        int cWidth = 0;
        int cHeight = 0;
        MarginLayoutParams cParams = null;

        // 用于计算左边两个childView的高度
        int lHeight = 0;
        // 用于计算右边两个childView的高度，最终高度取二者之间大值
        int rHeight = 0;

        // 用于计算上边两个childView的宽度
        int tWidth = 0;
        // 用于计算下面两个childiew的宽度，最终宽度取二者之间大值
        int bWidth = 0;


        /**
         * 根据childView计算的出的宽和高，以及设置的margin计算容器的宽和高，主要用于容器是warp_content时
         */
        for (int i = 0; i < mCount; i++) {
            View childView = getChildAt(i);
            cWidth = childView.getHeight();
            cHeight = childView.getWidth();
            cParams = (MarginLayoutParams) childView.getLayoutParams();
            if (i == 1 || i == 0) {
                tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
            }
            if (i == 3 || i == 2) {
                bWidth += cWidth + cParams.rightMargin + cParams.leftMargin;
            }
            if (i == 0 || i == 2) {
                lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
            }
            if (i == 1 || i == 3) {
                rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
            }
        }
        relHeight = Math.max(lHeight, rHeight);
        relWidth = Math.max(tWidth, bWidth);

        Log.e(TAG, "-->meature--" + "cWidth:" + cWidth + ",cHeight:" + cHeight + ",tWidth:"
                + tWidth + ",bWidth:" + bWidth + ",lHeight:" + lHeight + ",rHeight:" + rHeight);

        /**
         * 如果是wrap_content设置为我们计算的值
         * 否则：直接设置为父容器计算的值 */
        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? width : relWidth,
                (MeasureSpec.EXACTLY == heigthMode) ? heigth : relHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int mCount = getChildCount();
        int mWidth = 0;
        int mHeight = 0;
        MarginLayoutParams mParam = null;

//        遍历所有的childView，根据childView的宽和高以及margin，然后分别将0，1，2，3位置的childView依次设置到左上、右上、左下、右下的位置。
        for (int i = 0; i < mCount; i++) {
            View childView = getChildAt(i);
            mWidth = childView.getMeasuredWidth();
            mHeight = childView.getMeasuredHeight();
            mParam = (MarginLayoutParams) childView.getLayoutParams();
            int left = 0, right = 0, top = 0, bottom = 0;
            switch (i) {
                case 0:
                    left = mParam.leftMargin;
                    top = mParam.topMargin;
                    break;
                case 1:
                    left = getWidth() - mWidth - mParam.leftMargin - mParam.rightMargin;
                    top = mParam.topMargin;
                    break;
                case 2:
                    left = mParam.leftMargin;
                    top = getHeight() - mParam.bottomMargin - mHeight;
                    break;
                case 3:
                    top = getHeight() - mParam.bottomMargin - mHeight;
                    left = getWidth() - mWidth - mParam.leftMargin - mParam.rightMargin;
                    break;
            }
            right = left + mWidth;
            bottom = top + mHeight;
            Log.e(TAG, "CHILD--" + i + "--->" + ",mWidth:" + mWidth + ",mHeight:" + mHeight +
                    ",left:" + left + ",right:" + right + ",top:" + top + ",bottom:" + bottom);
            childView.layout(left, top, right, bottom);
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
//    @Override
//    protected ViewGroup.LayoutParams generateDefaultLayoutParams()
//    {
//        Log.e(TAG, "generateDefaultLayoutParams");
//        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
//                LayoutParams.MATCH_PARENT);
//    }
//
//    @Override
//    protected ViewGroup.LayoutParams generateLayoutParams(
//            ViewGroup.LayoutParams p)
//    {
//        Log.e(TAG, "generateLayoutParams p");
//        return new MarginLayoutParams(p);
//    }
}
